package com.liuzh.kaoyanspringboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liuzh.kaoyanspringboot.common.Result;
import com.liuzh.kaoyanspringboot.config.AuthAccess;
import com.liuzh.kaoyanspringboot.entity.Comment;
import com.liuzh.kaoyanspringboot.entity.Files;
import com.liuzh.kaoyanspringboot.entity.Message;
import com.liuzh.kaoyanspringboot.service.MessageService;
import com.liuzh.kaoyanspringboot.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 刘志鸿
 * @since 2023-03-22
 */
@RestController
@RequestMapping("/message")
public class MessageController {

    @Autowired
    private MessageService messageService;

    // 新增或更新
    @PostMapping
    public Result save(@RequestBody Message message) {
        return Result.success(messageService.saveOrUpdate(message));
    }

    // 根据 id 删除
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        return Result.success(messageService.removeById(id));
    }

    // 批量删除
    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        return Result.success(messageService.removeBatchByIds(ids));
    }
    // 批量已读
    @PostMapping("/old/batch")
    public Result oldBatch(@RequestBody List<Integer> ids) {
        return Result.success(messageService.oldBatchByIds(ids));
    }

    //查询所有数据
    @GetMapping
    public Result findAll() {
        return Result.success(messageService.list());
    }

    @GetMapping("/isNew")
    public Result findByIsNew(@RequestParam boolean isNew,
                              @RequestParam(defaultValue = "") String SNickname,
                              @RequestParam(defaultValue = "") String RNickname,
                              @RequestParam(defaultValue = "") String type) {
        return Result.success(messageService.findByIsNew(isNew, SNickname, RNickname, type));
    }

    // 根据 id 查询
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(messageService.getById(id));
    }

    //简单的分页查询
    @GetMapping("/easyPage")
    public Result findEPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize) {
        QueryWrapper<Message> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        return Result.success(messageService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam(defaultValue = "") String SNickname,
                           @RequestParam(defaultValue = "") String RNickname,
                           @RequestParam(defaultValue = "") String type) {
        return Result.success( messageService.findPage(new Page<>(pageNum,pageSize), SNickname, RNickname, type) );
    }

    /**
     * 查询回复信息
     * @return
     */
    @GetMapping("/myReply")
    public Result myReply() {
        List<Comment> replyComments = messageService.findReplyComment();
        return Result.success(replyComments);
    }

    /**
     * 获取关注我的人
     * @param userId
     * @return
     */
    @GetMapping("/concern/{userId}")
    public Result concernMe(@PathVariable Integer userId) {
        return Result.success(messageService.concernMe(userId));
    }
    /**
     * 获取未处理的消息数量
     * @return
     */
    @GetMapping("/myNewMessage")
    public Result myNewMessage() {
        return Result.success(messageService.getNewsByUserId(TokenUtils.getCurrentUser().getId()));
    }

}

